require "common/Define"
require "framework/BaseController"
require "FairyGUI"

JoystickMain = BaseController:New({mControllerName = "JoystickMain"});

function JoystickMain:Main()
	-- body
	JoystickMain:Awake();
end

function JoystickMain:Awake()
	Application.targetFrameRate = 60;
	Stage.inst.onKeyDown:Add(self.OnKeyDown, self);

	-- attack pacakge, package in resource or assetbundle
	local packageName = "UI/Joystick";
	UIPackage.AddPackage(packageName);

	local componentName = "Main";
	self.mainComponent = UIPackage.CreateObject("Joystick", componentName).asCom;
	GRoot.inst:AddChild( self.mainComponent );

	GRoot.inst:SetContentScaleFactor(1134, 640, FairyGUI.UIContentScaler.ScreenMatchMode.MatchWidthOrHeight);

	self:OnViewCreated(self.mainComponent);
end

function JoystickMain:InitView()
	-- body
	self._text = self.mainComponent:GetChild("n9").asTextField;

	self._joystick = JoystickModule(self.mainComponent);
	self._joystick.onMove:Add(self.__joystickMove, self);
	self._joystick.onEnd:Add(self.__joystickEnd, self);
end

function JoystickMain:__joystickMove(context)
	-- body
	local degree = context.data;
	self._text.text = "" .. degree;
end

function JoystickMain:__joystickEnd(context)
	-- body
	self._text.text = "";
end

function JoystickMain:OnKeyDown(context)
 	-- body
 	if (context.inputEvent.keyCode == KeyCode.Escape) then
		print "Exit Game!"
		Application.Quit();
	end
end